package io.github.talelin.latticy.controller.v1.ag;

import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.latticy.dto.ag.exceptionDataPush.CreateOrUpdateExceptionDataPushDTO;
import io.github.talelin.latticy.model.ag.ExceptionDataPushDO;
import io.github.talelin.latticy.service.ag.ExceptionDataPushService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.Positive;
import java.util.List;

@Validated
@RestController
@RequestMapping("/v1/ag/ExceptionDataPush")
public class ExceptionDataPushController {
    @Autowired
    ExceptionDataPushService dataPushService;
    @GetMapping("/{id}")
    public ExceptionDataPushDO getExceptionData(@PathVariable(value = "id") @Positive(message = "请输入正整数")int id){
        ExceptionDataPushDO exceptionData =dataPushService.getById(id);
        if (exceptionData==null){
            throw new NotFoundException("未找到异常数据");
        }
        return exceptionData;
    }
    @GetMapping("/list")
    public List<ExceptionDataPushDO> getExceptionDatas(){
        List<ExceptionDataPushDO> exceptionDatas = dataPushService.getExceptionDatas();
        if (exceptionDatas==null){
            throw new NotFoundException("未找到异常数据");
        }
        return exceptionDatas;
    }
    @PostMapping("/create")
    public CreatedVO createExceptionData(@RequestBody @Validated CreateOrUpdateExceptionDataPushDTO validator){
        dataPushService.createExceptionData(validator);
        return new CreatedVO("创建异常数据成功");
    }
    @DeleteMapping("/{id}")
    @PermissionMeta(value = "删除异常数据", module = "异常数据推送", mount = true)
    @GroupRequired
    public DeletedVO deletedExceptionData(@PathVariable("id") @Positive(message = "{id}") int id){
        ExceptionDataPushDO room = dataPushService.getById(id);
        if (room == null){
            throw new NotFoundException("异常数据未找到",10022);
        }
        dataPushService.deleteById(room.getId());
        return new DeletedVO(16);
    }

    @PutMapping("/{id}")
    public UpdatedVO updatedExceptionData(@PathVariable("id") @Positive(message = "{id.positive}") Integer id,
                                 @RequestBody @Validated CreateOrUpdateExceptionDataPushDTO validator){
        ExceptionDataPushDO exceptionData = dataPushService.getById(id);
        if (exceptionData == null){
            throw new NotFoundException("异常数据未找到",10022);
        }
        dataPushService.updataExceptionData(exceptionData,validator);
        return new UpdatedVO("更新异常数据成功");
    }
}
